use tauri::{
    AppHandle, CustomMenuItem, Manager, Menu, MenuItem, Submenu, SystemTray,
    SystemTrayEvent, SystemTrayMenu, SystemTrayMenuItem, WindowMenuEvent,
};

/// https://tauri.app/zh/v1/guides/features/system-tray
/// 创建系统托盘
pub fn create_system_tray() -> SystemTray {
    let quit = CustomMenuItem::new("quit".to_string(), "退出");
    let hide = CustomMenuItem::new("hide".to_string(), "隐藏");
    let show = CustomMenuItem::new("show".to_string(), "显示");
    let tray_menu = SystemTrayMenu::new()
        .add_item(show)
        .add_item(hide)
        .add_native_item(SystemTrayMenuItem::Separator)
        .add_item(quit);

    let tray = SystemTray::new().with_menu(tray_menu);
    tray
}

/// https://tauri.app/zh/v1/guides/features/system-tray#listening-to-system-tray-events
/// 托盘右键菜单点击处理函数
pub fn tray_menu_item_click_handle(app: &AppHandle, _event: &SystemTrayEvent, id: &str) {
    // let item_handle = app.tray_handle().get_item(id);
    match id {
        "hide" => {
            // 显示则隐藏
            let window = app.get_window("main").unwrap();
            if window.is_visible().is_ok() {
                window.hide().unwrap();
            }
        }
        "show" => {
            let window = app.get_window("main").unwrap();

            // 隐藏则显示
            if let Ok(flog) = window.is_visible() {
                if !flog {
                    window.show().unwrap();
                }
            };
        }
        "quit" => {
            std::process::exit(0);
        }
        _ => {}
    };
}

/// 托盘左键点击处理函数
pub fn tray_click_handle(app: &AppHandle) {
    let window = app.get_window("main").unwrap();

    // 隐藏则显示
    if let Ok(flog) = window.is_visible() {
        if !flog {
            window.show().unwrap();
            return;
        }
    }

    // 切换最大/最小化
    if let Ok(flog) = window.is_maximized() {
        if flog {
            window.unmaximize().unwrap(); // 取消最大化
        } else {
            window.maximize().unwrap(); // 最大化
        }
    };
}

/// https://tauri.app/zh/v1/guides/features/menu
/// 创建菜单
pub fn create_menu() -> Menu {
    let hide = CustomMenuItem::new("hide".to_string(), "隐藏");
    let close = CustomMenuItem::new("close".to_string(), "取消");
    let quit = CustomMenuItem::new("quit".to_string(), "退出");
    // 添加子项
    let submenu = Submenu::new(
        "多级菜单",
        Menu::new().add_item(hide).add_item(close).add_item(quit),
    );

    Menu::new()
        .add_item(CustomMenuItem::new("test", "一级菜单"))
        .add_submenu(submenu)
        // 系统自带菜单
        .add_native_item(MenuItem::Copy)
        .add_native_item(MenuItem::Cut)
        .add_native_item(MenuItem::Separator)
        .add_native_item(MenuItem::Paste)
}

/// https://tauri.app/zh/v1/guides/features/menu#listening-to-events-on-custom-menu-items
/// 监听菜单事件
pub fn menu_event_handle(event: &WindowMenuEvent) {
    match event.menu_item_id() {
        "hide" => event.window().hide().unwrap(),
        "close" => event.window().close().unwrap(),
        "quit" => std::process::exit(0),
        "test" => {
            println!("{}", "test");
        }
        _ => (),
    };
}
